package com.wnu.springboot.Controller;

import com.wnu.springboot.Util.StringUtils;
import com.wnu.springboot.db.bean.User;
import com.wnu.springboot.db.bean.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 20424
 * @Date 2020/8/5 17:57
 * @Version 1.0
 */
/*
 返回全为JSON：@RestController=@Controller+@ResponseBody
* 返回有视图时：使用@RestController=@Controller+@ResponseBody
* classpath:resources下的文件
* */
@RestController
@ImportResource(value = "classpath:config/spring-demo.xml")
public class LoginController {
    @Autowired
    DataSource dataSource;
    @Autowired
    User user;
    @RequestMapping("/login")
    public String login(){ return "HELLOWORLD!!!"; }
    @RequestMapping("/ReadFile")
    public String ReadFile(){
        return "连接用户名："+dataSource.getUsername();
    }
    @RequestMapping("/ReadUpFile")
    public String ReadUpFile(){
        String xml="classpath:config/spring-demo.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xml);
        StringUtils stringUtils= (StringUtils) applicationContext.getBean("StringUtil");
        return "id内容为："+stringUtils.getId();
    }
    @RequestMapping("/ReadymlFile")
    public String ReadYMLFile(){
        System.out.print(user.getUsername().get(0));
        System.out.print("："+user.getAge().get(0));
        return "yml文件中的用户名集合为："+user.getUsername()+"yml文件中的年龄集合为："+user.getAge();
    }
}
